package cc.ufinity.networkdiagnose.manager;

import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.Timer;
import java.util.TimerTask;

import org.xmlpull.v1.XmlPullParser;

import cc.ufinity.networkdiagnose.app.GlobalApp;
import cc.ufinity.networkdiagnose.util.HttpUtils;

import android.util.Xml;

public class GetAPInfo {

	public  DownApInfo downApinfo;
	public static DownApInfo downApinfo2;
	private String mac;
	private int lac;
	private int ci;
	private String phoneNO;
	private boolean isDD = false;
	private boolean isOver = false;
	private boolean isDD2 = false;
	private boolean isOver2 = false;
	private boolean isDD3 = false;
	private boolean isOver3 = false;
	
	public GetAPInfo(String ApMac) {
		if(GlobalApp.DEBUG) {
			mac = "00:3A:98:07:F1:B0";
		} else {
			mac = ApMac;
		}
	}

	public GetAPInfo(int lac,int ci) {
		this.lac = lac;
		this.ci = ci;
	}
	public GetAPInfo(String phoneNO, boolean b) {
		if(GlobalApp.DEBUG) {
			this.phoneNO = "15105506116";
		} else {
			this.phoneNO = phoneNO;
		}
		
	}
	public DownApInfo down(){
	 
		String url= GlobalApp.url_ReturnAPinfo + "mac=" + mac;
		//String url= GlobalApp.url_ReturnAPinfo + "mac=00:3A:98:07:F1:B0";
		InputStream is ;
		try {
			is = HttpUtils.getInputStream(url);
			downApinfo = parseFtpInfo(is);
			return  downApinfo;
		} catch (MalformedURLException e) {
			isOver = false;
			e.printStackTrace();
		} catch (Exception e) {
			isOver = false;
			e.printStackTrace();
		}
		return null;
	}
	
	public void start(){
		new Thread(){
			public void run(){

				while(!isDD) { 
					if(!isOver) {
						isOver = true;
						down();
					}
					try {
						Thread.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			} 
		}.start();
		
		final Timer timer = new Timer(true);
        timer.schedule(new TimerTask() {
			public void run() {
				isDD = true;
			}
		}, 3 * 60 * 1000);
	}
	
	 public DownApInfo parseFtpInfo(InputStream is) throws Exception {
	        XmlPullParser parser = Xml.newPullParser();
	        DownApInfo downApInfo = null;
	        parser.setInput(is, "UTF-8");
	        int type = parser.getEventType(); 
	        while (type != XmlPullParser.END_DOCUMENT) {
	            switch (type) {
	            case XmlPullParser.START_DOCUMENT:  
	            	isDD = true;
                	isDD2 = true;
	                break;
	            case XmlPullParser.START_TAG:
	                String tag = parser.getName();
	                if ("apinfo".equals(tag)) {
	                	downApInfo = new DownApInfo();
	                } else if ("ApMac".equals(tag)) {
	                    type = parser.next();
	                    downApInfo.setMac(DESManager.getDesString(parser.getText()));
	                } else if ("City".equals(tag)) {
	                    type = parser.next();
	                    downApInfo.setCity(DESManager.getDesString(parser.getText()));
	                }else if ("Town".equals(tag)) {
	                    type = parser.next();
	                    downApInfo.setTown(DESManager.getDesString(parser.getText()));
	                }else if ("ApName".equals(tag)) {
	                    type = parser.next();
	                    String port = DESManager.getDesString(parser.getText());
	                    downApInfo.setAPName(port);
	                }else if ("Wlan_ApName".equals(tag)) {
	                    type = parser.next();
	                    String port = DESManager.getDesString(parser.getText());
	                    downApInfo.setWlan_ApName(port);
	                }
	                break;
	            case XmlPullParser.END_TAG:
	                tag = parser.getName();
	                if ("apinfo".equals(tag)) { 
	                	GlobalApp.city = downApInfo.getCity();
	                	GlobalApp.town = downApInfo.getTown();
	                	System.out.println("city ------  town  " + GlobalApp.city + "  ====  " + GlobalApp.town);
	                }
	                break;
	            case XmlPullParser.END_DOCUMENT:  
	                break;
	            }
	            type = parser.next();
	        }
	        is.close();
	        return downApInfo;
	    }
	 
	 //=====================TD/GSM
	 public DownApInfo down2(){
		 
			String url= GlobalApp.url_ReturnAPinfo + "Lac=" + lac+"&CI="+ci;
			InputStream is ;
			try {
				is = HttpUtils.getInputStream(url);
				downApinfo2 = parseFtpInfo(is);
				if(downApinfo2 == null) {
					System.out.println("city:null ============hhhhh============= town:null");
				} else {
					System.out.println(downApinfo2.getCity()+"============hhhhh============="+downApinfo2.getTown());
				}
				return  downApinfo2;
			} catch (MalformedURLException e) {
				isOver2 = false;
				e.printStackTrace();
			} catch (Exception e) {
				isOver2 = false;
				e.printStackTrace();
			}
			return null;
		}
		
		public void start2(){
			new Thread(){
				public void run(){
	
					while(!isDD2) { 
						if(!isOver2) {
							isOver2 = true;
							down2();
						}
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				}
			}.start();
			final Timer timer = new Timer(true);
	        timer.schedule(new TimerTask() {
				public void run() {
					isDD2 = true;
				}
			}, 3 * 60 * 1000);
		} 
		
		
//==================================
		public String down3(){
			 
			String url= GlobalApp.url_GetCityBuyPhone + phoneNO;
			//String url= GlobalApp.url_ReturnAPinfo + "mac=00:3A:98:07:F1:B0";
			InputStream is ;
			try {
				is = HttpUtils.getInputStream(url);
				GlobalApp.CITY = parseCityInfo(is);
				return  GlobalApp.CITY;
			} catch (MalformedURLException e) {
				isOver3 = false;
				e.printStackTrace();
			} catch (Exception e) {
				isOver3 = false;
				e.printStackTrace();
			}
			return null;
		}
		
		public void start3(){
			new Thread(){
				public void run(){

					while(!isDD3) { 
						if(!isOver3) {
							isOver3 = true;
							down3();
						}
						try {
							Thread.sleep(1000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
					}
				} 
			}.start();
			
			final Timer timer = new Timer(true);
	        timer.schedule(new TimerTask() {
				public void run() {
					isDD3 = true;
				}
			}, 3 * 60 * 1000);
		}
		
		 public String parseCityInfo(InputStream is) throws Exception {
		        XmlPullParser parser = Xml.newPullParser();
		        String city = null;
		        parser.setInput(is, "UTF-8");
		        int type = parser.getEventType(); 
		        while (type != XmlPullParser.END_DOCUMENT) {
		            switch (type) {
		            case XmlPullParser.START_DOCUMENT:  
		            	isDD3 = true;
		                break;
		            case XmlPullParser.START_TAG:
		                String tag = parser.getName();
		                if ("City".equals(tag)) {
		                    type = parser.next();
		                    city = DESManager.getDesString(parser.getText());
		                    System.out.println("通过手机号码获取CITY：" + city);
		                }else if ("Town".equals(tag)) {
		                    type = parser.next();
		                }
		                break;
		            case XmlPullParser.END_TAG:
		                tag = parser.getName();
		                break;
		            case XmlPullParser.END_DOCUMENT:  
		                break;
		            }
		            type = parser.next();
		        }
		        is.close();
		        return city;
		    }
}
